/*
sound_tips.c
offer sound tips function realize
designed by lunanting

--------------------------
2021-02-01 10:58:19
create file
--------------------------

*/

#include "sound_tips.h"
#include "../voice.h"
#include "string.h"
#include "stdio.h"
#include "../../bsp/cli_cmd.h"

static TIPS tips={0};


static void xp_soundTips_play_thread(void *arg);
static int xp_soundTips_debug(char *type,char *fun,char *param);

/*
sound tips module init
return:
    0=success
*/
int xp_soundTips_init(void){
    int sta=0;
    sta=xp_voice_init();
    if(sta!=0)return -3;
    if(aos_sem_new(&tips.sem,0)!=0)return -1;
    if(aos_task_new("tips play",xp_soundTips_play_thread,NULL,2050)!=0)return -2;
    xp_cli_debug_add(xp_soundTips_debug);
    tips.init=1;
    println("sound tips module init success ~");
    return sta;
}


/*
play tips thread
*/
static void xp_soundTips_play_thread(void *arg){
    int sta=0;
    int count=0;

    while(1){
        count=0;
        aos_sem_wait(&tips.sem,AOS_WAIT_FOREVER);
        xp_voice_clr_source();
        for(int i=0;i<tips.cycle;i++){
            if(tips.stop==1){
                tips.stop=0;
                break;
            }
            if(count%50==0){
                sta=xp_voice_start(xp_soundTips_resource_path(tips.type),50);
                if(sta!=0){
                    perr("voice play error, code = %d",sta);
                }
            }
            count++;
            aos_msleep(100);
        }
    }
}


/*
set sound tips
param:
    type: tips type
    cycle: play cycle times
*/
void xp_soundTips_set(TIPS_type type,int cycle){
    tips.stop=1;
    tips.cycle=cycle;
    tips.type=type;
    if(type!=tips_null){
        aos_msleep(150);
        tips.stop=0;
        println("tips set :type = %d, cycle = %d",type,cycle);
        aos_sem_signal(&tips.sem);
    }
}



/*
get sound tips path string
param:
    type: tips type
return:string pointer
*/
char *xp_soundTips_resource_path(TIPS_type type){
    static char path[100];

    snprintf(path,99,"%s/%d.mp3",TIPS_path,(int)type);
    return path;
}



/*
clear tips
*/
void xp_soundTips_clear(void){
    tips.stop=1;
    xp_voice_clr_source();
}




/*
test all type
*/
void xp_soundTips_test(void){
    for(TIPS_type i=tips_startup;i<tips_common_end;i++){
        xp_soundTips_set(i,1);
        aos_msleep(3000);
    }
    for(TIPS_type i=tips_collision;i<tips_terminal_end;i++){
        xp_soundTips_set(i,1);
        aos_msleep(3000);
    }
    for(TIPS_type i=tips_washer_online;i<tips_station_end;i++){
        xp_soundTips_set(i,1);
        aos_msleep(3000);
    }
}



/************************************************debug******************************/
//sound tips debug
static int xp_soundTips_debug(char *type,char *fun,char *param){
    if(strcmp(type,"tips")!=0)return 0;
    if(strcmp(fun,"set")==0){
        xp_soundTips_set((TIPS_type)atoi(param),1);

    }
    else if(strcmp(fun,"test")==0){
        xp_soundTips_test();
    }
    else{
        return 0;
    }
    return 1;
}









// end of the file
